app.controller('fuzz_upnpCtrl',function ($scope,$http,$window,$interval,ngDialog,$state,ip_interface) {
    var height=$window.innerHeight/1.6;
    $scope.tbodyheight={
        "height":height+"px"
    }
    $scope.Mutator = {
        UnicodeBadUtf8Mutator : 3,
        DWORDSliderMutator : 4,
        BitFlipperMutator : 5,
        nullStringMutator : 6,
        UnicodeBomMutator : 7,
        changeChangeRangeSpecial : 8,
        changeExpandBuffer : 9,
        changeNullRange :10,
        changeChangeRange : 11
    }
    $http({
        method:'GET',
        url:'http://'+ip_interface+':8003/secsmarts/getFuzzInfo',
        //url:'http://'+ip_interface+':5000/ble',
    }).then(function successCallback (response) {
        testtable=response.data.data;
        console.log(testtable);
        $scope.upnptable=[];
        var len=testtable.length;
        for(var i=0;i<testtable.length;i++)
        {
            if(testtable[i]['remark']=='UPnP_device')
            {
                $scope.upnptable.push(testtable[i]);
            }
        }
    });
    //每隔3秒刷新一次数据,用于更新测试的进度
    var timer=$interval(function(){
        $http({
            method:'GET',
            url:'http://'+ip_interface+':8003/secsmarts/getFuzzInfo',
        }).then(function successCallback (response) {
            testtable=response.data.data;
            $scope.upnptable=[];
            var len=testtable.length;
            for(var i=0;i<testtable.length;i++)
            {
                if(testtable[i]['remark']=='UPnP_device')
                {
                    $scope.upnptable.push(testtable[i]);
                }
            }
            var height=$window.innerHeight/1.6;
            $scope.tbodyheight={
                "height":height+"px"
            }
            console.log("测试列表内容");
            console.log(response.data);
        });
    },1000);   //间隔3秒定时执行
    //路由切换时关闭定时器
    $scope.$on('$destroy',function(){
        $interval.cancel(timer);
    });
    $scope.onAddClick=function () {
        $http({
            method:'GET',
            url:'http://'+ip_interface+':8003/secsmarts/getDevices',
        }).then(function successCallback (response) {
                $scope.devicetable=response.data.data;
                $scope.deviceList=new Array();
                var cnt=0;
                for(var i=0;i<$scope.devicetable.length;i++)
                {
                    var remarkstr="";
                    remarkstr=$scope.devicetable[i].remark;
                    var remarklist=[];
                    remarklist=remarkstr.split(",");
                    for(var j=0;j<remarklist.length;j++)
                    {
                        if(remarklist[j]=="UPnP_device")
                        {
                            $scope.deviceList[cnt]={"mac":$scope.devicetable[i].mac,"ip":$scope.devicetable[i].ip,"remark":remarklist[j]};
                            cnt++;
                        }
                    };
                }
                if($scope.deviceList.length<1)
                {
                    ngDialog.open({
                        template:'<div>当前无该类型设备，不能添加测试</div>',
                        plain:true,
                        ClassName:'ngdialog-theme-default'
                    });
                    return;
                }
                //发完http请求打开弹窗（原）,修改：设备列表初始化完成以后才打开窗口
                ngDialog.open({
                    template:'popupTempl/selectdevicetemplate.html',
                    scope:$scope,
                    controller:function ($scope,$http,ip_interface) {
                        $scope.deviceList=$scope.$parent.deviceList;
                        $scope.confirm=function (value,test_count,timeout_time,selectedCar) {
                            console.log("确定添加测试");
                            console.log(selectedCar);
                            $scope.jsonBody={"mac":value.mac,"remark":[value.remark],"test_count":timeout_time,"timeout":test_count,"fuzztype":selectedCar};
                            console.log('confirmed,value-',$scope.jsonBody);
                            $http({
                                method:"POST",
                                url:"http://"+ip_interface+":8003/secsmarts/addFuzzInfo",
                                data:angular.toJson($scope.jsonBody),
                                headers:{'Content-Type': 'application/json;charset=UTF-8'}
                            }).then(function (response) {
                                console.log(response.data);
                                //$window.location.reload();
                                $http({
                                    method:'GET',
                                    url:'http://'+ip_interface+':8003/secsmarts/getFuzzInfo',
                                }).then(function successCallback (response) {
                                    //$scope.testtable=response.data.data;
                                    $scope.closeThisDialog();
                                });
                            },function (response) {
                                console.log("error");
                            });
                        };
                    },
                    ClassName:'ngdialog-theme-default'
                });
            },
            function (reason) {
                console.log("the error is:"+reason);
            }
        );
    };
    $scope.onChangeRunstateClick=function (online_status,id,state) {
        if(online_status!=1)
        {
            ngDialog.open({
                template:"<div>设备不在线</div>",
                ClassName:'ngdialog-theme-default',
                plain:true
            });
            return;
        }
        $scope.jsonBody={"id":id,"change_state":state};
        console.log('the state is');
        console.log(state);
        $http({
            method:"POST",
            url:"http://"+ip_interface+":8003/secsmarts/updateFuzzStatus",
            data:angular.toJson($scope.jsonBody),
            headers:{'Content-Type': 'application/json;charset=UTF-8'}
        }).then(function (response) {
            console.log('change state result:');
            console.log(response.data);
            $window.location.reload();
        },function (response) {
            console.log("error response",response);
        })
    };
    //这个删除目前没用是因为数据原因，id字段全部为NULL，所以不能确定到底选择了哪个，就无法删除
    $scope.onDeleteClick=function (id) {
        $scope.jsonBody={"data":[{"id":id}]};
        $http({
            method:"POST",
            url:"http://"+ip_interface+":8003/secsmarts/delFuzzInfo",
            data:angular.toJson($scope.jsonBody),
            headers:{'Content-Type': 'application/json;charset=UTF-8'}
        }).then(function (response) {
            for(var key in response.data)
                console.log("++++"+key);
            console.log("the data is "+response.data.status);
            $window.location.reload();
        },function (response) {
            console.log("error");
        })
    };
    $scope.onViewClick=function (runstate,id) {
        //取消测试是否已经完成的判断，如果测试中出现了异常，也可以查看报告
        $scope.reportdetail="";
        $scope.jsonBody={"id":id};
        $http({
            method:"POST",
            url:"http://"+ip_interface+":8003/secsmarts/getFuzzReportById",
            data:angular.toJson($scope.jsonBody),
            headers:{'Content-Type': 'application/json;charset=UTF-8'}
        }).then(function (response) {
            $scope.reportdetail=response.data;
            console.log(response.data);
        },function (response) {
            console.log("error");
        });
        ngDialog.open({
            template:'popupTempl/reportwindowTemplate.html',
            scope:$scope,
            ClassName:'ngdialog-theme-default',
            width:900,
            height:900
        });
    };
    $scope.onDetailClick=function (runstate,id) {
        $state.go('fuzz_upnp_detail',{id:id});
    };
});
//弃用，转移到内部控制器
app.controller('insideCtrl',function ($scope,$http,ip_interface) {
    console.log("这是insidecontorller");
    $scope.deviceList=$scope.$parent.deviceList;
    console.log("inside执行");
    $scope.confirm=function (value,test_count,timeout_time) {
        console.log("确定添加测试")
        $scope.jsonBody={"mac":value.mac,"test_count":test_count,"timeout":timeout_time};
        console.log('confirmed,value-',$scope.jsonBody);
        $http({
            method:"POST",
            url:"http://"+ip_interface+":8003/secsmarts/addFuzzInfo",
            data:angular.toJson($scope.jsonBody),
            headers:{'Content-Type': 'application/json;charset=UTF-8'}
        }).then(function (response) {
            console.log(response.data);
            //$window.location.reload();
            $http({
                method:'GET',
                url:'http://'+ip_interface+':8003/secsmarts/getFuzzInfo',
            }).then(function successCallback (response) {
                $scope.testtable=response.data.data;
            });
        },function (response) {
            console.log("error");
        });
    };
});